from Tkinter import *
import sys


class ClassFormWindow(Toplevel):
    """
    represents the window containing the form for a class
    """
    
    def __init__(self, parentControl, crc):
        Toplevel.__init__(self, parentControl)
        self.parentControl = parentControl
        self.frame = Frame(self)
        if crc == None:
            self.mode = "create"
            self.classid = None
        else:
            self.mode = "edit"
            self.classid = crc.id
        
        self.frame.label1 = Label(self, text="Nom:")
        self.frame.label1.grid(row = 0, column = 0)
        self.frame.label2 = Label(self, text="Responsibilitees")
        self.frame.label2.grid(row = 1, column = 0)
        self.frame.label3 = Label(self, text="Collaborations:")
        self.frame.label3.grid(row = 2, column = 0)
        self.frame.textBox = Entry(self, width = 50)
        self.frame.textBox.grid(row = 0, column = 1)
        self.frame.responsibilityList = Listbox(self, width = 50)
        self.frame.responsibilityList.grid(row = 1, column = 1)
        self.frame.collaborationList = Listbox(self, width = 50)
        self.frame.collaborationList.grid(row = 2, column = 1)
        self.frame.buttonOk = Button(self, text="Ok", command=self.createOk)
        self.frame.buttonOk.grid(row = 3, column = 0)
        self.frame.buttonCancel = Button(self, text="Annuler", command=self.createQuit)
        self.frame.buttonCancel.grid(row = 3, column = 1)
        self.frame.buttonEditResponsibilities = Button(self, text="Modifier", command=self.editResponsibilities)
        self.frame.buttonEditResponsibilities.grid(row = 1, column = 3)
        self.frame.buttonEditCollaborations = Button(self, text="Modifier", command=self.editCollaborations)
        self.frame.buttonEditCollaborations.grid(row = 2, column = 3)
        self.frame.buttonAddResponsibilities = Button(self, text="Ajouter", command=self.addResponsibilities)
        self.frame.buttonAddResponsibilities.grid(row = 1, column = 2)
        self.frame.buttonAddCollaborations = Button(self, text="Ajouter", command=self.addCollaborations)
        self.frame.buttonAddCollaborations.grid(row = 2, column = 2)
        
        if crc != None:
            self.frame.textBox.insert(END, crc.name)
            for responsibility in crc.responsibilities:
                self.frame.responsibilityList.insert(0,responsibility)
            for collaboration in crc.collaborations:
                self.frame.collaborationList.insert(0,collaboration)
     
    def createOk(self):
        self.parentControl.listener.onConfirmClassCreation(self.frame.textBox.get(), self.frame.responsibilityList.get(0,END), self.frame.collaborationList.get(0,END), self.classid, self.mode)

    def addResponsibilities(self):
        self.frame.responsibilityList.insert(ACTIVE,self.parentControl.listener.onEditListboxRequest())
    
    def addCollaborations(self):
        self.frame.collaborationList.insert(ACTIVE,self.parentControl.listener.onEditListboxRequest())
        
    def createQuit(self):
        self.parentControl.listener.onQuitClassFormWindow() 
        
    def editResponsibilities(self):
        newName = self.parentControl.listener.onEditListboxRequest(self.frame.responsibilityList.get(ACTIVE))
        if newName != None:
            self.frame.responsibilityList.delete(ACTIVE)
            self.frame.responsibilityList.insert(ACTIVE,newName)
    
    def editCollaborations(self):
        newName = self.parentControl.listener.onEditListboxRequest(self.frame.collaborationList.get(ACTIVE))
        if newName != None:
            self.frame.collaborationList.delete(ACTIVE)
            self.frame.collaborationList.insert(ACTIVE,newName)
    
                
if __name__ == "__main__":
    root = Tk()
    classFormWindowObj = ClassFormWindow(root,2)
    root.mainloop()
    
        